@@ -10,6 +10,7 @@ gem "rufus-scheduler", :require => false |
||
10 | 10 |
gem 'json', '>= 1.7.7' |
11 | 11 |
gem 'jsonpath' |
12 | 12 |
gem 'twilio-ruby' |
13 |
+gem 'ruby-growl' |
|
13 | 14 |
|
14 | 15 |
gem 'delayed_job' |
15 | 16 |
gem 'delayed_job_active_record'#, "~> 0.3.3" # newer was giving a strange MySQL error |
@@ -0,0 +1,62 @@ |
||
1 |
+require 'ruby-growl' |
|
2 |
+ |
|
3 |
+module Agents |
|
4 |
+ class GrowlAgent < Agent |
|
5 |
+ attr_reader :growler |
|
6 |
+ |
|
7 |
+ cannot_be_scheduled! |
|
8 |
+ cannot_create_events! |
|
9 |
+ |
|
10 |
+ description <<-MD |
|
11 |
+ The GrowlAgent sends any events it receives to a Growl GNTP server immediately. |
|
12 |
+ |
|
13 |
+ It is assumed that events have a `message` or `text` key, which will hold the body of the growl notification, and a `subject` key, which will have the headline of the Growl notification. You can use Event Formatting Agent if your event does not provide these keys. |
|
14 |
+ |
|
15 |
+ Set `expected_receive_period_in_days` to the maximum amount of time that you'd expect to pass between Events being received by this Agent. |
|
16 |
+ MD |
|
17 |
+ |
|
18 |
+ def default_options |
|
19 |
+ { |
|
20 |
+ 'growl_server' => 'localhost', |
|
21 |
+ 'growl_password' => '', |
|
22 |
+ 'growl_app_name' => 'HuginnGrowl', |
|
23 |
+ 'growl_notification_name' => 'Notification', |
|
24 |
+ 'expected_receive_period_in_days' => "2" |
|
25 |
+ } |
|
26 |
+ end |
|
27 |
+ |
|
28 |
+ def working? |
|
29 |
+ last_receive_at && last_receive_at > options['expected_receive_period_in_days'].to_i.days.ago && !recent_error_logs? |
|
30 |
+ end |
|
31 |
+ |
|
32 |
+ def validate_options |
|
33 |
+ unless options['growl_server'].present? && options['expected_receive_period_in_days'].present? |
|
34 |
+ errors.add(:base, "growl_server and expected_receive_period_in_days are required fields") |
|
35 |
+ end |
|
36 |
+ end |
|
37 |
+ |
|
38 |
+ def register_growl |
|
39 |
+ @growler = Growl.new options['growl_server'], options['growl_app_name'], "GNTP" |
|
40 |
+ @growler.password = options['growl_password'] |
|
41 |
+ @growler.add_notification options['growl_notification_name'] |
|
42 |
+ end |
|
43 |
+ |
|
44 |
+ def notify_growl(subject, message) |
|
45 |
+ @growler.notify(options['growl_notification_name'],subject,message) |
|
46 |
+ end |
|
47 |
+ |
|
48 |
+ def receive(incoming_events) |
|
49 |
+ register_growl |
|
50 |
+ incoming_events.each do |event| |
|
51 |
+ message = (event.payload['message'] || event.payload['text']).to_s |
|
52 |
+ subject = event.payload['subject'].to_s |
|
53 |
+ if message.present? && subject.present? |
|
54 |
+ log "Sending Growl notification '#{subject}': '#{message}' to #{options['growl_server']} with event #{event.id}" |
|
55 |
+ notify_growl(subject,message) |
|
56 |
+ else |
|
57 |
+ log "Event #{event.id} not sent, message and subject expected" |
|
58 |
+ end |
|
59 |
+ end |
|
60 |
+ end |
|
61 |
+ end |
|
62 |
+end |
@@ -0,0 +1,122 @@ |
||
1 |
+require 'spec_helper' |
|
2 |
+ |
|
3 |
+describe Agents::GrowlAgent do |
|
4 |
+ before do |
|
5 |
+ @checker = Agents::GrowlAgent.new(:name => 'a growl agent', |
|
6 |
+ :options => { :growl_server => 'localhost', |
|
7 |
+ :growl_app_name => 'HuginnGrowlApp', |
|
8 |
+ :growl_password => 'mypassword', |
|
9 |
+ :growl_notification_name => 'Notification', |
|
10 |
+ :expected_receive_period_in_days => '1' }) |
|
11 |
+ @checker.user = users(:bob) |
|
12 |
+ @checker.save! |
|
13 |
+ |
|
14 |
+ stub.any_instance_of(Growl).notify |
|
15 |
+ |
|
16 |
+ @event = Event.new |
|
17 |
+ @event.agent = agents(:bob_weather_agent) |
|
18 |
+ @event.payload = { :subject => 'Weather Alert!', :message => 'Looks like its going to rain' } |
|
19 |
+ @event.save! |
|
20 |
+ end |
|
21 |
+ |
|
22 |
+ describe "#working?" do |
|
23 |
+ it "checks if events have been received within the expected receive period" do |
|
24 |
+ @checker.should_not be_working # No events received |
|
25 |
+ Agents::GrowlAgent.async_receive @checker.id, [@event.id] |
|
26 |
+ @checker.reload.should be_working # Just received events |
|
27 |
+ two_days_from_now = 2.days.from_now |
|
28 |
+ stub(Time).now { two_days_from_now } |
|
29 |
+ @checker.reload.should_not be_working # More time has passed than the expected receive period without any new events |
|
30 |
+ end |
|
31 |
+ end |
|
32 |
+ |
|
33 |
+ describe "validation" do |
|
34 |
+ before do |
|
35 |
+ @checker.should be_valid |
|
36 |
+ end |
|
37 |
+ |
|
38 |
+ it "should validate presence of of growl_server" do |
|
39 |
+ @checker.options[:growl_server] = "" |
|
40 |
+ @checker.should_not be_valid |
|
41 |
+ end |
|
42 |
+ |
|
43 |
+ it "should validate presence of expected_receive_period_in_days" do |
|
44 |
+ @checker.options[:expected_receive_period_in_days] = "" |
|
45 |
+ @checker.should_not be_valid |
|
46 |
+ end |
|
47 |
+ end |
|
48 |
+ |
|
49 |
+ describe "register_growl" do |
|
50 |
+ it "should set the password for the Growl connection from the agent options" do |
|
51 |
+ @checker.register_growl |
|
52 |
+ @checker.growler.password.should eql(@checker.options[:growl_password]) |
|
53 |
+ end |
|
54 |
+ |
|
55 |
+ it "should add a notification to the Growl connection" do |
|
56 |
+ called = false |
|
57 |
+ any_instance_of(Growl) do |obj| |
|
58 |
+ called = true |
|
59 |
+ mock(obj).add_notification(@checker.options[:growl_notification_name]) |
|
60 |
+ end |
|
61 |
+ |
|
62 |
+ @checker.register_growl |
|
63 |
+ called.should be_true |
|
64 |
+ end |
|
65 |
+ end |
|
66 |
+ |
|
67 |
+ describe "notify_growl" do |
|
68 |
+ before do |
|
69 |
+ @checker.register_growl |
|
70 |
+ end |
|
71 |
+ |
|
72 |
+ it "should call Growl.notify with the correct notification name, subject, and message" do |
|
73 |
+ message = "message" |
|
74 |
+ subject = "subject" |
|
75 |
+ called = false |
|
76 |
+ any_instance_of(Growl) do |obj| |
|
77 |
+ called = true |
|
78 |
+ mock(obj).notify(@checker.options[:growl_notification_name],subject,message) |
|
79 |
+ end |
|
80 |
+ @checker.notify_growl(subject,message) |
|
81 |
+ called.should be_true |
|
82 |
+ end |
|
83 |
+ end |
|
84 |
+ |
|
85 |
+ describe "receive" do |
|
86 |
+ def generate_events_array |
|
87 |
+ events = [] |
|
88 |
+ (2..rand(7)).each do |
|
89 |
+ events << @event |
|
90 |
+ end |
|
91 |
+ return events |
|
92 |
+ end |
|
93 |
+ |
|
94 |
+ it "should call register_growl once regardless of number of events received" do |
|
95 |
+ mock.proxy(@checker).register_growl.once |
|
96 |
+ @checker.receive(generate_events_array) |
|
97 |
+ end |
|
98 |
+ |
|
99 |
+ it "should call notify_growl one time for each event received" do |
|
100 |
+ events = generate_events_array |
|
101 |
+ events.each do |event| |
|
102 |
+ mock.proxy(@checker).notify_growl(event.payload['subject'], event.payload['message']) |
|
103 |
+ end |
|
104 |
+ @checker.receive(events) |
|
105 |
+ end |
|
106 |
+ |
|
107 |
+ it "should not call notify_growl if message or subject are missing" do |
|
108 |
+ event_without_a_subject = Event.new |
|
109 |
+ event_without_a_subject.agent = agents(:bob_weather_agent) |
|
110 |
+ event_without_a_subject.payload = { :message => 'Looks like its going to rain' } |
|
111 |
+ event_without_a_subject.save! |
|
112 |
+ |
|
113 |
+ event_without_a_message = Event.new |
|
114 |
+ event_without_a_message.agent = agents(:bob_weather_agent) |
|
115 |
+ event_without_a_message.payload = { :subject => 'Weather Alert YO!' } |
|
116 |
+ event_without_a_message.save! |
|
117 |
+ |
|
118 |
+ mock.proxy(@checker).notify_growl.never |
|
119 |
+ @checker.receive([event_without_a_subject,event_without_a_message]) |
|
120 |
+ end |
|
121 |
+ end |
|
122 |
+end |